const fs = require('fs')
const path = require('path')

let size = 0

function calculateDirSize(dirPath) {
  dirPath = path.resolve(__dirname, dirPath)
  const statObj = fs.statSync(dirPath);
  if (statObj.isFile()) {
    return statObj.size;
  }
  // 同步方式
  let files = fs.readdirSync(dirPath)
  files = files.filter(file => !file.startsWith('.'))
  for(let i = 0; i < files.length; i ++) {
    filePath = path.join(dirPath, files[i]);
    let res = calculateDirSize(filePath);
    if (res !== undefined) {
      size += res;
    }
  }
}

calculateDirSize('./test')
console.log(size);